<template>
  <div class="pf-period-manage pf-bgwhite pf-p-10">
    <div class="pf-mt-20 pf-text-left">
      <el-button icon="Refresh" @click="select"></el-button>
      <el-button type="primary" @click="create">创建</el-button>
      <el-button @click="del">删除</el-button>
    </div>
    <pf-table class="pf-mt-20" :config="classTableConfig" :data="classTableData">
      <el-table-column label="操作" fixed="right" width="80">
        <template #default="scope">
          <el-tooltip effect="light" placement="left" trigger="hover">
            <component :is="'Menu'" class="el-button--text" style="width: 18px"></component>
            <template #content>
              <div class="pf-flex pf-flex-column pf-flex-0">
                <el-button @click="handleClick(scope.row, 'edit')" link type="primary" size="mini"
                  >编辑</el-button
                >
              </div>
            </template>
          </el-tooltip>
        </template>
      </el-table-column>
    </pf-table>
    <el-dialog title="课程周期信息" v-model="show">
      <pf-form ref="classFormDom" :config="classFormConfig" :model="classFormData"></pf-form>
      <template #footer>
        <div class="pf-text-right">
          <el-button type="primary" @click="confirmCreate">确认</el-button>
          <el-button @click="show = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref, Ref } from 'vue';
import { useNotice } from '@/components/element-plus/notice';
import { emptyForm, emptyTable, FormModel, SysDict, TableModel, useData } from 'pf-component';
import { TableNameEnum } from '@/constants/enum/table-name.enum';
import { FormNameEnum } from '@/constants/enum/form-name.enum';
import { useClassPeriod } from '@/views/teach/period/useClassPeriod';
import { parseTime } from '@/constants/util/objects-utils';

export default defineComponent({
  name: 'Period',
  setup() {
    const { message } = useNotice();
    const classTableConfig: Ref<TableModel> = ref(emptyTable);
    const classTableData: Ref<any[]> = ref([]);

    const show: Ref<boolean> = ref(false);
    const classFormConfig: Ref<FormModel> = ref(emptyForm);
    const classFormDom = ref(null);
    const classFormData: Ref = ref(null as any);
    onMounted(() => {
      Promise.all([
        useData().loadTable([{ name: TableNameEnum.dcClassPeriod, config: classTableConfig }]),
        useData().loadForm([
          { name: FormNameEnum.dcClassPeriod, config: classFormConfig, info: classFormData }
        ])
      ]).then(ress => {
        if (ress.findIndex(res => !res) === -1) {
          select();
        }
      });
    });
    const { list, update } = useClassPeriod();
    const select = () => {
      list().then(res => {
        classTableData.value = res;
      });
    };

    const create = () => {
      classFormData.value = useData().toFormValue(classFormConfig.value);
      show.value = true;
    };
    const confirmCreate = () => {
      (classFormDom.value as any).validate((val: boolean) => {
        if (val) {
          const data = { ...classFormData.value };
          data.endTime = parseTime(JSON.stringify(data.endTime));
          data.startTime = parseTime(JSON.stringify(data.startTime));
          update(data).then(res => {
            if (res) {
              show.value = false;
              select();
            }
          });
        } else {
          message.error('表单校验失败');
        }
      });
    };
    const del = () => {
      message.error('待完善');
    };
    const handleClick = (data: SysDict, cmd: string) => {
      switch (cmd) {
        case 'edit':
          classFormData.value = data;
          show.value = true;
          break;
        default:
          message.error('未定义的操作！');
      }
    };
    return {
      select,
      classTableData,
      classTableConfig,
      handleClick,
      create,
      confirmCreate,
      del,
      show,
      classFormDom,
      classFormConfig,
      classFormData
    };
  }
});
</script>
<style scoped lang="scss">
.#{$prefix} {
  &-period-manage {
    //margin: 10px;
    //padding: 0;
    overflow-y: auto;
    height: calc(100% - 60px);
  }
}
</style>
